package com.shop.controller;

import com.shop.bean.Admin;
import com.shop.bean.User;
import com.shop.service.IAdminService;
import com.shop.util.LayData;
import com.shop.util.Page;
import com.shop.util.SYS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("admin")
public class AdminController {

    @Autowired
    IAdminService adminService;

    @RequestMapping("getadmininfo")
    @ResponseBody
    public LayData getLoginAdmin(HttpSession session){
        Admin loginadmin=(Admin)session.getAttribute(SYS.SYSTEM_LOGIN_ADMIN);
        if(loginadmin==null){
            throw new RuntimeException("未登录");
        }
        return LayData.success(200,"",loginadmin);
    }

    @RequestMapping("reloadPassword")
    @ResponseBody
    public LayData reloadPassword(HttpSession session, Integer id){

        adminService.reloadPassword(id);
        return LayData.success(200, "修改成功");
    }

    @RequestMapping("list")
    @ResponseBody
    public LayData getList(Page page, String text) {
        List<Admin> list=adminService.selectAdmins(page, text);
        int count=adminService.getCount(text);
        return new LayData(count, list);
    }

    @RequestMapping("delete")
    @ResponseBody
    public LayData delete(Integer id,HttpSession session) {

        Admin loginadmin = (Admin) session.getAttribute(SYS.SYSTEM_LOGIN_ADMIN);
        if(id == loginadmin.getId()){
            throw new RuntimeException("无法删除自己！！！");
        }
        adminService.deleteAdmin(id);

        return new LayData(200,"删除成功！！！",1,id);
    }
}
